package com.atguigu.chapter11.window;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.GroupWindow;
import org.apache.flink.table.api.Session;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.lit;

/**
 * Author: Pepsi
 * Date: 2023/8/24
 * Desc:
 */
public class Flink02_Window_Group_2 {
    public static void main(String[] args) {

        // 获取流执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStream<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1000L, 10),
                new WaterSensor("sensor_1", 2000L, 20),
                new WaterSensor("sensor_2", 3000L, 30),
                new WaterSensor("sensor_1", 4000L, 40),
                new WaterSensor("sensor_1", 5000L, 50),
                new WaterSensor("sensor_1", 6000L, 60)
        )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((ws,ts)->ws.getTs())
                );

        // 获取表执行环境，在这之前要获取流的执行环境作为参数传过来
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

        // 把流转换成表
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts"), $("vc"), $("et").rowtime());

        tEnv.createTemporaryView("sensor",table);

        // sql中使用滚动窗口 tumble(字段名, interval '5' second)
        /*tEnv.sqlQuery("select " +
                "id," +
                "tumble_start(et,interval '5' second) stt," +
                "tumble_end(et,interval '5' second) edt," +
                "sum(vc) vc_sum " +
                "from sensor " +
                "group by id, tumble(et, interval '5' second) ")
                .execute().print();*/

        // 滑动窗口
        /*tEnv.sqlQuery("select " +
                "id," +
                " hop_start(et,interval '2' second,interval '5' second) stt," +
                " hop_end(et,interval '2' second,interval '5' second) edt, " +
                "sum(vc) vc_sum " +
                "from sensor " +
                "group by id, hop(et,interval '2' second,interval '5' second) ")
                .execute().print();*/


        tEnv.sqlQuery("select " +
                "id," +
                " session_start(et,interval '2' second) stt," +
                " session_end(et,interval '2' second) edt, " +
                "sum(vc) vc_sum " +
                "from sensor " +
                "group by id, session(et,interval '2' second) ")
                .execute().print();
    }
}
